/**
 * 平台账号管理API - 后台管理
 * 提供平台账号的查询和管理功能
 */

const Framework = require("../../framework/node-core-framework.js");
const plaAccountService = require('../services/pla_account_service');

module.exports = {
  'GET /pla_account/getById': async (ctx) => {
    const { id } = ctx.getQuery();
    const accountData = await plaAccountService.getAccountById(id);
    return ctx.success(accountData);
  },

  'POST /account/detail': async (ctx) => {
    const { id } = ctx.getBody();
    const accountData = await plaAccountService.getAccountById(id);
    return ctx.success(accountData);
  },
  /**
   * @swagger
   * /admin_api/account/list:
   *   post:
   *     summary: 获取账号列表
   *     description: 分页获取所有平台账号
   *     tags: [后台-账号管理]
   *     requestBody:
   *       required: true
   *       content:
   *         application/json:
   *           schema:
   *             type: object
   *             properties:
   *               page:
   *                 type: integer
   *                 description: 页码
   *               pageSize:
   *                 type: integer
   *                 description: 每页数量
   *               key:
   *                 type: string
   *                 description: 搜索字段
   *               value:
   *                 type: string
   *                 description: 搜索值
   *               platform_type:
   *                 type: string
   *                 description: 平台类型ID
   *               is_online:
   *                 type: boolean
   *                 description: 是否在线
   *     responses:
   *       200:
   *         description: 获取成功
   */
  'POST /account/list': async (ctx) => {
    const body = ctx.getBody();
    const { key, value, platform_type, is_online } = body;
    const { limit, offset } = ctx.getPageSize();

    const result = await plaAccountService.getAccountList({
      key,
      value,
      platform_type,
      is_online,
      limit,
      offset
    });

    return ctx.success(result);
  },

  /**
   * @swagger
   * /admin_api/account/add:
   *   post:
   *     summary: 新增账号
   *     description: 创建新的平台账号
   *     tags: [后台-账号管理]
   *     requestBody:
   *       required: true
   *       content:
   *         application/json:
   *           schema:
   *             type: object
   *             required:
   *               - name
   *               - sn_code
   *               - platform_type
   *               - login_name
   *             properties:
   *               name:
   *                 type: string
   *                 description: 账户名
   *               sn_code:
   *                 type: string
   *                 description: 设备SN码
   *               platform_type:
   *                 type: string
   *                 description: 平台类型ID
   *               login_name:
   *                 type: string
   *                 description: 登录名
   *               pwd:
   *                 type: string
   *                 description: 密码
   *               keyword:
   *                 type: string
   *                 description: 搜索关键词
   *               search_url:
   *                 type: string
   *                 description: 搜索页网址
   *     responses:
   *       200:
   *         description: 创建成功
   */
  'POST /account/add': async (ctx) => {
    const body = ctx.getBody();
    const account = await plaAccountService.createAccount(body);
    return ctx.success({ message: '账号创建成功', data: account });
  },

  /**
   * @swagger
   * /admin_api/account/update:
   *   post:
   *     summary: 更新账号信息
   *     description: 更新平台账号信息
   *     tags: [后台-账号管理]
   *     requestBody:
   *       required: true
   *       content:
   *         application/json:
   *           schema:
   *             type: object
   *             required:
   *               - id
   *             properties:
   *               id:
   *                 type: integer
   *                 description: 账号ID
   *     responses:
   *       200:
   *         description: 更新成功
   */
  'POST /account/update': async (ctx) => {
    const body = ctx.getBody();
    const { id, ...updateData } = body;
    await plaAccountService.updateAccount(id, updateData);
    return ctx.success({ message: '账号信息更新成功' });
  },

  /**
   * @swagger
   * /admin_api/account/delete:
   *   post:
   *     summary: 删除账号
   *     description: 删除指定的平台账号（软删除）
   *     tags: [后台-账号管理]
   *     requestBody:
   *       required: true
   *       content:
   *         application/json:
   *           schema:
   *             type: object
   *             required:
   *               - id
   *             properties:
   *               id:
   *                 type: integer
   *                 description: 账号ID
   *     responses:
   *       200:
   *         description: 删除成功
   */
  'POST /account/delete': async (ctx) => {
    const body = ctx.getBody();
    const { id } = body;
    await plaAccountService.deleteAccount(id);
    return ctx.success({ message: '账号删除成功' });
  },

  /**
   * @swagger
   * /admin_api/account/stopTasks:
   *   post:
   *     summary: 停止账号的所有任务
   *     description: 停止指定账号的所有待执行和正在执行的任务
   *     tags: [后台-账号管理]
   *     requestBody:
   *       required: true
   *       content:
   *         application/json:
   *           schema:
   *             type: object
   *             required:
   *               - id
   *             properties:
   *               id:
   *                 type: integer
   *                 description: 账号ID
   *               sn_code:
   *                 type: string
   *                 description: 设备SN码（可选，如果提供id则不需要）
   *     responses:
   *       200:
   *         description: 停止成功
   */
  'POST /account/stopTasks': async (ctx) => {
    const body = ctx.getBody();
    const result = await plaAccountService.stopTasks(body);
    return ctx.success(result);
  },


  /**
   * @swagger
   * /admin_api/pla_account/tasks:
   *   get:
   *     summary: 获取账号的任务列表
   *     description: 根据账号ID获取该账号的所有任务列表（支持分页）
   *     tags: [后台-账号管理]
   *     parameters:
   *       - in: query
   *         name: id
   *         required: true
   *         schema:
   *           type: integer
   *         description: 账号ID
   *       - in: query
   *         name: page
   *         schema:
   *           type: integer
   *         description: 页码
   *       - in: query
   *         name: pageSize
   *         schema:
   *           type: integer
   *         description: 每页数量
   *     responses:
   *       200:
   *         description: 获取成功
   */
  'GET /pla_account/tasks': async (ctx) => {
    const { id } = ctx.getQuery();
    const { limit, offset } = ctx.getPageSize();

    const result = await plaAccountService.getAccountTasks({
      id,
      limit,
      offset
    });

    return ctx.success(result);
  },

  /**
   * @swagger
   * /admin_api/pla_account/commands:
   *   get:
   *     summary: 获取账号的指令列表
   *     description: 根据账号ID获取该账号的所有指令列表（支持分页）
   *     tags: [后台-账号管理]
   *     parameters:
   *       - in: query
   *         name: id
   *         required: true
   *         schema:
   *           type: integer
   *         description: 账号ID
   *       - in: query
   *         name: page
   *         schema:
   *           type: integer
   *         description: 页码
   *       - in: query
   *         name: pageSize
   *         schema:
   *           type: integer
   *         description: 每页数量
   *     responses:
   *       200:
   *         description: 获取成功
   */
  'GET /pla_account/commands': async (ctx) => {
    const { id } = ctx.getQuery();
    const { limit, offset } = ctx.getPageSize();

    const result = await plaAccountService.getAccountCommands({
      id,
      limit,
      offset
    });

    return ctx.success(result);
  },

  /**
   * @swagger
   * /admin_api/pla_account/runTask:
   *   post:
   *     summary: 执行账号指令
   *     description: 为指定账号直接执行指令（如用户登录、获取简历等）
   *     tags: [后台-账号管理]
   *     requestBody:
   *       required: true
   *       content:
   *         application/json:
   *           schema:
   *             type: object
   *             required:
   *               - id
   *               - taskType
   *             properties:
   *               id:
   *                 type: integer
   *                 description: 账号ID
   *               taskType:
   *                 type: string
   *                 description: 指令类型: get_login_qr_code-登录检查, get_resume-获取简历, search_jobs-搜索岗位
   *               taskName:
   *                 type: string
   *                 description: 指令名称
   *     responses:
   *       200:
   *         description: 指令执行成功
   */
  'POST /pla_account/runTask': async (ctx) => {
    const body = ctx.getBody();
    const task = await plaAccountService.runTask(body);
    return ctx.success(task);
  },
  /**
   * @swagger
   * /admin_api/pla_account/runCommand:
   *   post:
   *     summary: 执行账号指令
   *     description: 为指定账号直接执行指令（如用户登录、获取简历等）
   *     tags: [后台-账号管理]
   *     requestBody:
   *       required: true
   *       content:
   *         application/json:
   *           schema:
   *             type: object
   *             required:
   *               - id
   *               - commandType
   *             properties:
   *               id:
   *                 type: integer
   *                 description: 账号ID
   *               commandType:
   *                 type: string
   *                 description: 指令类型: get_login_qr_code-登录检查, get_resume-获取简历, search_jobs-搜索岗位
   *               commandName:
   *                 type: string
   *                 description: 指令名称
   *     responses:
   *       200:
   *         description: 指令执行成功
   */
  'POST /pla_account/runCommand': async (ctx) => {
    const body = ctx.getBody();
    const result = await plaAccountService.runCommand(body);
    return ctx.success(result);
  },

  /**
   * @swagger
   * /admin_api/pla_account/commandDetail:
   *   get:
   *     summary: 获取指令详情
   *     description: 根据账号ID和指令ID获取指令的详细信息
   *     tags: [后台-账号管理]
   *     parameters:
   *       - in: query
   *         name: accountId
   *         required: true
   *         schema:
   *           type: integer
   *         description: 账号ID
   *       - in: query
   *         name: commandId
   *         required: true
   *         schema:
   *           type: integer
   *         description: 指令ID
   *     responses:
   *       200:
   *         description: 获取成功
   */
  'GET /pla_account/commandDetail': async (ctx) => {
    const { accountId, commandId } = ctx.getQuery();
    const commandDetail = await plaAccountService.getCommandDetail({
      accountId,
      commandId
    });
    return ctx.success(commandDetail);
  },

  /**
   * @swagger
   * /admin_api/pla_account/parseLocation:
   *   post:
   *     summary: 解析地址并更新经纬度
   *     description: 根据账号ID解析地址，获取经纬度并更新到账号信息中
   *     tags: [后台-账号管理]
   *     requestBody:
   *       required: true
   *       content:
   *         application/json:
   *           schema:
   *             type: object
   *             required:
   *               - id
   *             properties:
   *               id:
   *                 type: integer
   *                 description: 账号ID
   *               address:
   *                 type: string
   *                 description: 地址（可选，如果不提供则使用账号中的地址）
   *     responses:
   *       200:
   *         description: 解析成功
   */
  'POST /pla_account/parseLocation': async (ctx) => {
  
      const body = ctx.getBody();
      const result = await plaAccountService.parseLocation(body);
      return ctx.success(result);
   
  },

  /**
   * @swagger
   * /admin_api/pla_account/batchParseLocation:
   *   post:
   *     summary: 批量解析地址并更新经纬度
   *     description: 批量解析多个账号的地址，获取经纬度并更新到账号信息中
   *     tags: [后台-账号管理]
   *     requestBody:
   *       required: true
   *       content:
   *         application/json:
   *           schema:
   *             type: object
   *             required:
   *               - ids
   *             properties:
   *               ids:
   *                 type: array
   *                 items:
   *                   type: integer
   *                 description: 账号ID数组
   *     responses:
   *       200:
   *         description: 批量解析完成
   */
  'POST /pla_account/batchParseLocation': async (ctx) => {
    const body = ctx.getBody();
    const { ids } = body;
    const result = await plaAccountService.batchParseLocation(ids);
    return ctx.success(result);
  }
};

